home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 403 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.7 KB

  1. From: phalpern@truffle.ultranet.com (Pablo Halpern)
  2. Message-ID: <4gfg2f$iga@caesar.ultra.net>
  3. X-Original-Date: Wed, 21 Feb 1996 16:08:45 GMT
  4. Path: in2.uu.net!bounce-back
  5. Date: 21 Feb 96 16:13:11 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: Q: Generic Callbacks -- "Object->*func(...)"
  9. Organization: UltraNet Communications, Inc.
  10. References: <4fti32$p3p@bcarh8ab.bnr.ca>
  11. X-Newsreader: Forte Agent .99b.113
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMStEq+EDnX0m9pzZAQERygF/UO4VTWYjxJ/ToYm40NnAgzsovSXK5vtn
  14.     6FvwgPRlKUq4QK5isx/WAaTY49B8YPJA
  15.     =2Yzj
  16.  
  17. "brian (b.c.) white" <bcwhite@bnr.ca> wrote:
  18.  
  19. >Does the C++ standard allow for a generic callback to be specified?
  20. >
  21. >Basically, I'd like to be able to pass an arbitrary object and function of
  22. >that object to be called at some later time.  For example:
  23.  
  24. This is a problem I'd like to see addressed directly in the language or
  25. in the standard library (although I doubt it will be).
  26.  
  27. A former collegue of mine suggested to me that the semantics of taking
  28. the address of a member function in C++ could be much more powerfull
  29. than they are. In C++, a function is not bound until it is invoked using
  30. the object.function() or pointer->function() construct. What he
  31. suggested is that there be a special type of pointer that refers to a
  32. bound function. (He called it a closure, although I'm not sure it meets
  33. the precise definition of closure, since parameters are not specified.)
  34. Usage would be something like this:
  35.  
  36.   // The use of the "bound" keyword, below, is for illustrative purposes
  37.   // only and is not intended as a syntax proposal.
  38.   typedef bound void (*boundfunc)(args);  // Pointer to bound function
  39.  
  40.   class firstClass
  41.   {
  42.     public:
  43.       void func1(args);
  44.       void func2(args);
  45.   };
  46.  
  47.   class secondClass
  48.   {
  49.     public:
  50.       void func3(args);
  51.   };
  52.  
  53.   f()
  54.   {
  55.     firstClass firstObj;
  56.     secondClass secondObj;
  57.     boundfunc pf = &(firstObj.func1);
  58.     pf(actual args);  // calls firstObj.func1(actual args)
  59.     pf = &(secondObj.func3);
  60.     pf(actual args);  // calls secondObj.func3(actual args)
  61.   }
  62.  
  63. The address of functions will be either a normal pointer to function, a
  64. pointer to member function, or a pointer to bound function as follows:
  65.  
  66.    &globalFunc         // pointer to global/static function
  67.    &firstClass::func2  // pointer to member function (of firstClass)
  68.    &firstObj.func2     // pointer to bound function (bound to firstObj)
  69.    &pointer->func1     // pointer to bound function (bound to *pointer)
  70.  
  71. I believe that the above could be a reasonable addition to the language:
  72.  
  73. 1. It is useful
  74. 2. I think it is easy to implement. The internal structure of a bound
  75. pointer is basically an object pointer and a pointer to the actual code
  76. of the member function (no need for offsets or vtbl references, since
  77. the binding has already been done). All object offset calculations are
  78. done before the object pointer is stored. Global and static function
  79. pointers can be stored in a bound pointer by setting the object pointer
  80. to NULL.
  81. 3. It will not break existing code (the &(object.function) notation is
  82. currently illegal).
  83. 4. I'm not sure that this cannot be achieved any other way, but I think
  84. that it may not be achievable efficiently any other way.
  85.  
  86. -------------------------------------------------------------
  87. Pablo Halpern                   phalpern@truffle.ultranet.com
  88.  
  89. I am self-employed. Therefore, my opinions *do* represent 
  90. those of my employer.
  91. ---
  92. [ To submit articles: try just posting with your news-reader.
  93.                       If that fails, use mailto:std-c++@ncar.ucar.edu
  94.   FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  95.   Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  96.   Comments? mailto:std-c++-request@ncar.ucar.edu.
  97. ]
  98.